import time
import hashlib
import json
import requests
import re
import pymongo

def tb_1688(liveid, qk_1688, num):
    for i in range(3):
        try:
            # url = 'https://h5api.m.1688.com/h5/mtop.taobao.powermsg.h5.msg.pulltopicstat/1.0/'
            # url = 'https://h5api.m.taobao.com/h5/mtop.taobao.powermsg.h5.msg.pulltopicstat/1.0/'
            # url = 'https://h5api.m.taobao.com/h5/mtop.taobao.powermsg.msg.pulltopicstat/1.0/'
            # url = 'https://h5api.m.taobao.com/h5/mtop.taobao.powermsg.msg.pulltopicstat/1.0/'
            # ------------------------
            url = 'https://h5api.m.taobao.com/h5/mtop.taobao.powermsg.h5.msg.pulltopicuserlist/1.0/'

            headers = {
                "Referer": "http://huodong.m.taobao.com/act/talent/live.html",
                "Sec-Fetch-Mode": "no-cors",
                'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
                # 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36',
            }
            # data = {"topic": liveid, "sdkversion":"h5_1.3.7"}
            data = {"topic": liveid, "sdkversion": "h5_1.3.7", "offset": 1570523860933, "pagesize": 30}
            data = str(data)
            APPKEY = '12574478'
            params = {
                'AntiCreep': 'false',
                'jsv': '2.5.0',
                'appKey': APPKEY,
                't': int(time.time() * 1000),
                'sign': 'FAKE_SIGN_WITH_ANYTHING',
                'api': 'mtop.taobao.powermsg.h5.msg.pulltopicstat',
                'v': '1.0',
                'type': 'json',
                'dataType': 'json',
                "sdkversion": "h5_1.3.7",
                'callback': 'mtopjsonp{}'.format(num),
                'data': data
            }
            headers['Cookie'] = qk_1688
            token_with_time = re.findall('_m_h5_tk=(.*?);', qk_1688)[0]
            token = token_with_time.split('_')[0]
            t2 = str(int(time.time() * 1000))
            c = '&'.join([token, t2, APPKEY, data])
            m = hashlib.md5()
            m.update(c.encode('utf-8'))
            params.update({'t': t2, 'sign': m.hexdigest()})

            r2 = requests.get(url, params=params, headers=headers)
            # return json.loads(r2.text)['data']
            # print(params)
            return json.loads(r2.text)
        except:
            continue


apis = {
    "powermsg": "mtop.taobao.powermsg.h5.msg.pullnativemsg", # done
    "countmsg": "mtop.taobao.powermsg.h5.msg.count",
    "pulltopicstatmsg": "mtop.taobao.powermsg.h5.msg.pulltopicstat", # done
    "pulltopicuserlistmsg": "mtop.taobao.powermsg.h5.msg.pulltopicuserlist",
    "pullhistorymsg": "mtop.taobao.powermsg.h5.msg.pullhistorymsg",
    "sendMsg": "mtop.taobao.powermsg.h5.msg.sendmsg",
    "subscribemsg": "mtop.taobao.powermsg.h5.msg.subscribe",
    "unsubscribemsg": "mtop.taobao.powermsg.h5.msg.unsubscribe",
    "ack": "mtop.taobao.powermsg.monitor.ack.upload"
}


if __name__ == '__main__':
    myclient = pymongo.MongoClient("mongodb://47.98.146.210:27017")
    mydb_cookies = myclient["QK_GOODS"]
    mycol_cookies = mydb_cookies['qk_cookies']
    cookiee = mycol_cookies.find({}).sort([('_id', -1)]).limit(1)
    for cook in cookiee:
        qk_1688 = cook['qk_taobao']
        # qk_1688 = cook['qk_h5_taobao']

    for i in range(0, 10):
        # res = tb_1688("1f5ac3a4-ceb6-401b-bf63-35df58349168", qk_1688, i)
        # res = tb_1688("01029d61-5c17-4530-b9fe-546745909aca", qk_1688, i)
        res = tb_1688("0fd1eabb-c7cd-412b-95f0-85d6e523fc25", qk_1688, i)
        # res = tb_1688("f41cb3e0-1859-4955-98e2-f597957df64e", qk_1688, i)
        print(res)
        time.sleep(2)